package jp.dodododo.aop.util;

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class ClassUtil {
	public static Method getDeclaredMethod(Class<?> clazz, String methodName, Class<?>[] paramTypes) {
		try {
			Method declaredMethod = clazz.getDeclaredMethod(methodName, paramTypes);
			declaredMethod.setAccessible(true);
			return declaredMethod;
		} catch (Throwable t) {
			String message = "WARN : The method was not able to be enhance. : " + clazz.getName() + "#" + methodName + "() : "
					+ t;
			System.err.println(message);
			return null;
		}
	}

	public static Constructor<?> getDeclaredConstructor(Class<?> clazz, Class<?>[] paramTypes) {
		try {
			return clazz.getDeclaredConstructor(paramTypes);
		} catch (Throwable t) {
			String message = "WARN : The constructor was not able to be enhance. : " + clazz.getName() + " : " + t;
			System.err.println(message);
			return null;
		}
	}

	public static Class<?> getArrayClass(Class<?> componentClass, int size) {
		return Array.newInstance(componentClass, size).getClass();
	}
}
